Skip to content

gh-150880: Normalize Windows scandir wildcard paths#152906

Merged
zooba merged 1 commit into
python:mainfrom
zainnadeem786:fix/scandir-normalize-base-path
Jul 3, 2026
Merged

gh-150880: Normalize Windows scandir wildcard paths#152906
zooba merged 1 commit into
python:mainfrom
zainnadeem786:fix/scandir-normalize-base-path

Conversation

@zainnadeem786

@zainnadeem786 zainnadeem786 commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Summary

On Windows, os.listdir() and os.scandir() append a wildcard before calling FindFirstFileW().

For paths with a trailing space, this changes the path from a final-component trailing-space case into an internal path component, so Windows no longer trims the space. This makes listdir() and scandir() behave differently from APIs such as open(), os.stat(), and os.path.exists().

This PR normalizes non-extended Windows base paths with GetFullPathNameW() before appending the wildcard used for enumeration.

Extended paths beginning with \\?\ continue to bypass normalization.

Changes

  • Normalize only the wildcard path used by Windows listdir() and scandir().
  • Preserve existing DirEntry.path construction behavior.
  • Preserve extended-path semantics.
  • Add regression coverage for trailing-space directory paths.

Tests

PCbuild\build.bat -p x64 -d
PCbuild\amd64\python_d.exe -m unittest -v test.test_os.test_os.TestScandir.test_windows_trailing_space_path
PCbuild\amd64\python_d.exe -m test test_os
PCbuild\amd64\python_d.exe -m test test_pathlib test_ntpath
git diff --check

Close #150880

@zooba zooba added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jul 3, 2026
@zooba zooba merged commit 1b4135a into python:main Jul 3, 2026
62 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @zainnadeem786 for the PR, and @zooba for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Jul 3, 2026

Copy link
Copy Markdown

GH-152963 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jul 3, 2026
@bedevere-app

bedevere-app Bot commented Jul 3, 2026

Copy link
Copy Markdown

GH-152964 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jul 3, 2026
@bedevere-app

bedevere-app Bot commented Jul 3, 2026

Copy link
Copy Markdown

GH-152965 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Jul 3, 2026
zooba pushed a commit that referenced this pull request Jul 3, 2026
GH-152906) (GH-152965)

gh-150880: Normalize paths on Windows before appending wildcard (GH-152906)

This ensures that we don't turn a "valid" path with trailing spaces into an invalid path with embedded spaces.
(cherry picked from commit 1b4135a)

Co-authored-by: Zain Nadeem <zainnadeemzainnadeem80@gmail.com>
zooba pushed a commit that referenced this pull request Jul 3, 2026
GH-152906) (GH-152964)

gh-150880: Normalize paths on Windows before appending wildcard (GH-152906)

This ensures that we don't turn a "valid" path with trailing spaces into an invalid path with embedded spaces.
(cherry picked from commit 1b4135a)

Co-authored-by: Zain Nadeem <zainnadeemzainnadeem80@gmail.com>
zooba pushed a commit that referenced this pull request Jul 3, 2026
GH-152906) (GH-152963)

gh-150880: Normalize paths on Windows before appending wildcard (GH-152906)

This ensures that we don't turn a "valid" path with trailing spaces into an invalid path with embedded spaces.
(cherry picked from commit 1b4135a)

Co-authored-by: Zain Nadeem <zainnadeemzainnadeem80@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

On Windows scandir handles paths different from other filesystem APIs

2 participants